<template>
  <el-card class="search-card" shadow="never">
    <el-form :model="searchForm" inline>
      <slot :searchForm="searchForm" />
      <el-form-item>
        <el-button type="primary" @click="handleSearch"> 搜索 </el-button>
        <el-button @click="handleReset"> 重置 </el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup>
import { reactive } from "vue";

const props = defineProps({
  defaultValues: {
    type: Object,
    default: () => ({}),
  },
});

const emit = defineEmits(["search", "reset"]);

const searchForm = reactive({ ...props.defaultValues });

const handleSearch = () => {
  emit("search", searchForm);
};

const handleReset = () => {
  Object.keys(searchForm).forEach((key) => {
    searchForm[key] = props.defaultValues[key] || "";
  });
  emit("reset", searchForm);
};

defineExpose({
  searchForm,
});
</script>

<style lang="scss" scoped>
.search-card {
  margin-bottom: 20px;

  :deep(.el-card__body) {
    padding: 20px;
  }
}
</style>
